home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wil4c10.zip / PAINT.C < prev    next >
C/C++ Source or Header  |  1997-07-26  |  6KB  |  257 lines

  1. /*** paint.c ***/
  2.  
  3. /* BLACK on WHITE [no color] */
  4.  
  5. #include "windows.h"
  6. #include <string.h>
  7. #include "paint.h"
  8. #include "ascii.h"
  9.  
  10. extern HWND hMainWnd;
  11.  
  12. #define MIN(a,b) ((a<=b)?(a):(b))
  13. #define MAX(a,b) ((a>=b)?(a):(b))
  14.  
  15. /* PRIVATE variables */
  16.  
  17. static int TheRow = 0;     /* current row */
  18. static int TheCol = 0;     /* current col */
  19. static int TopRow;
  20. static int LeftCol;
  21. static int RightCol;
  22. static char Buffer[NROWS][NCOLS];  /* display buffer */
  23. static char *RowPtr[NROWS];        /* array of row pointers */
  24. static TEXTMETRIC tm;
  25. static int CharHeight;
  26. static int CharWidth;
  27. static char EndOfLine[1] = {LF};
  28.  
  29. /* PRIVATE functions */
  30.  
  31. void DoTheScroll(void)
  32. {int Row;
  33.  int Col;
  34.  char *Ptr;
  35.  RECT rect;
  36.  /* scroll display buffer */
  37.  TheRow = NROWS-1;
  38.  Ptr = RowPtr[0];
  39.  for(Row=0;Row<NROWS-1;Row++) RowPtr[Row] = RowPtr[Row+1];
  40.  RowPtr[NROWS-1] = Ptr;
  41.  for(Col=0;Col<NCOLS;Col++) *Ptr++ = ' ';
  42.  /* scroll the display */
  43.  ScrollWindow(hMainWnd,0,0-CharHeight,NULL,NULL);
  44.  /* invalidate last row */
  45.  rect.left = 0;
  46.  rect.top  = CharHeight * (NROWS-2);
  47.  rect.right  = CharWidth * (RightCol+1);
  48.  rect.bottom = CharHeight * (NROWS-1);
  49.  InvalidateRect(hMainWnd,&rect,TRUE);
  50.  /* reset boundary */
  51.  TopRow = TheRow;
  52.  LeftCol = TheCol;
  53.  RightCol = TheCol;
  54. } /* end DoTheScroll */
  55.  
  56. static void ClearBuffer(void)
  57. {int Row;
  58.  int Col;
  59.  /* clear screen buffer */
  60.  for(Row=0;Row<NROWS;Row++)
  61.    {for(Col=0;Col<NCOLS;Col++) Buffer[Row][Col] = ' ';
  62.     RowPtr[Row] = &Buffer[Row][0];
  63.    }
  64.  TheRow = 0;
  65.  TheCol = 0;
  66. }
  67.  
  68. /* PUBLIC functions */
  69.  
  70. int PaintGetRowPos(void)
  71. {return (TheRow*CharHeight);
  72. }
  73.  
  74. int PaintGetColPos(void)
  75. {return (TheCol*CharWidth);
  76. }
  77.  
  78. int PaintGetRow(void)
  79. {return TheRow;
  80. }
  81.  
  82. int PaintGetCol(void)
  83. {return TheCol;
  84. }
  85.  
  86. void PaintSetRow(int Row)
  87. {RECT Rect;
  88.  if((unsigned)Row<NROWS)
  89.    {
  90.     Rect.left   = 0;
  91.     Rect.right  = CharWidth * (RightCol+1);
  92.     Rect.top    = CharHeight * MIN(TheRow,Row);
  93.     Rect.bottom = CharHeight * MAX(TheRow+1,Row+1);
  94.     InvalidateRect(hMainWnd,&Rect,TRUE);
  95.     TheRow = Row;
  96.     SetCaretPos(CharWidth*TheCol,CharHeight*TheRow);
  97.    }
  98. }
  99.  
  100. void PaintSetCol(int Col)
  101. {RECT Rect;
  102.  if((unsigned)Col<NCOLS)
  103.    {
  104.     Rect.left   = CharWidth * MIN(TheCol,Col);
  105.     Rect.right  = CharWidth * MAX(TheCol+1,Col+1);
  106.     Rect.top    = 0;
  107.     Rect.bottom = CharHeight * TheRow;
  108.     InvalidateRect(hMainWnd,&Rect,TRUE);
  109.     TheCol = Col;
  110.     SetCaretPos(CharWidth*TheCol,CharHeight*TheRow);
  111.    }
  112. }
  113.  
  114. void PaintInit(void)
  115. {
  116.  HDC hDC;
  117.  hDC = GetDC(hMainWnd);
  118.  SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  119.  GetTextMetrics(hDC,&tm);
  120.  CharHeight = tm.tmHeight + tm.tmExternalLeading;
  121.  CharWidth = tm.tmMaxCharWidth;
  122.  ReleaseDC(hMainWnd,hDC);
  123.  /* initialize screen buffer */
  124.  ClearBuffer();
  125. } /* end InitPaint */
  126.  
  127. void PaintClearScreen(void)
  128. {HBRUSH hBrush;
  129.  RECT   Rect;
  130.  HDC    hDC;
  131.  ClearBuffer();
  132.  hDC = GetDC(hMainWnd);
  133.  hBrush = CreateSolidBrush(RGB(255,255,255));
  134.  SetRect(&Rect, 0,0, NCOLS*CharWidth, NROWS*CharHeight);
  135.  FillRect(hDC,&Rect,hBrush);
  136.  SetCaretPos(0,0);
  137.  InvalidateRect(hMainWnd,&Rect,TRUE);
  138.  ReleaseDC(hMainWnd,hDC);
  139. }
  140.  
  141. void PaintClearEOL(void)
  142. {int    i;
  143.  HBRUSH hBrush;
  144.  RECT   Rect;
  145.  HDC    hDC;
  146.  char   *Ptr;
  147.  Ptr = RowPtr[TheRow] + TheCol;
  148.  for(i=TheCol;i<NCOLS;i++) *(Ptr++) = ' ';
  149.  hDC = GetDC(hMainWnd);
  150.  hBrush = CreateSolidBrush(RGB(255,255,255));
  151.  SetRect(&Rect,
  152.          TheCol*CharWidth, TheRow*CharHeight,
  153.          NCOLS*CharWidth,  (TheRow+1)*CharHeight);
  154.  FillRect(hDC,&Rect,hBrush);
  155.  InvalidateRect(hMainWnd,&Rect,TRUE);
  156.  ReleaseDC(hMainWnd,hDC);
  157. }
  158.  
  159. /* paint the display */
  160.  
  161. void PaintMain(HDC hDC,PAINTSTRUCT *ps)
  162. {int Row;
  163.  int FirstRow;
  164.  int FirstCol;
  165.  int NbrRows;
  166.  int NbrCols;
  167.  int ColWidth;
  168.  int X;
  169.  int Y;
  170.  RECT rect;
  171.  /* compute row & col stuff */
  172.  FirstRow = ps->rcPaint.top  / CharHeight;
  173.  FirstCol = ps->rcPaint.left / CharWidth;
  174.  NbrRows = (CharHeight -1 + ps->rcPaint.bottom - ps->rcPaint.top)  / CharHeight;
  175.  ColWidth = ps->rcPaint.right  - ps->rcPaint.left;
  176.  NbrCols = MIN(NCOLS,(1+ColWidth) / CharWidth);
  177.  X = ps->rcPaint.left;
  178.  /* consider each row */
  179.  for(Row=FirstRow;Row<FirstRow+NbrRows;Row++)
  180.    {/* paint part of row */
  181.     if((Row>=0)&&(Row<NROWS))
  182.       {/* good row number */
  183.        Y = CharHeight*Row;
  184.        /* compute bounding rectangle */
  185.        rect.left = X;
  186.        rect.top  = Y;
  187.        rect.right  = X + ColWidth;
  188.        rect.bottom = Y + CharHeight;
  189.        /* paint it */
  190.        SetBkMode(hDC,OPAQUE);
  191.        ExtTextOut(hDC,X,Y,ETO_OPAQUE|ETO_CLIPPED,&rect,RowPtr[Row]+FirstCol,NbrCols,NULL);
  192.       }
  193.    }
  194. } /* end PaintMain */
  195.  
  196. void WriteTheString(LPSTR String, int Count)
  197. {int i;
  198.  char TheChar;
  199.  RECT rect;
  200.  TopRow = TheRow;
  201.  LeftCol = TheCol;   /* leftmost col of String */
  202.  RightCol = TheCol;  /* rightmost col of String */
  203.  /* examine each char in turn */
  204.  for(i=0;i<Count;i++)
  205.    {TheChar = *String++;
  206.     switch(TheChar)
  207.      {case BS:
  208.         if(TheCol>0)
  209.           {*(RowPtr[TheRow]+TheCol) = ' ';
  210.            TheCol--;
  211.           }
  212.         break;
  213.       case LF:
  214.         /* next line */
  215.         if(++TheRow>=NROWS) DoTheScroll();
  216.         /*break;*/
  217.       case CR:
  218.         TheCol = 0;
  219.         LeftCol = 0;
  220.         break;
  221.       default:
  222.         /* put char into display buffer */
  223.         *(RowPtr[TheRow]+TheCol) = (char)TheChar;
  224.         /* increment 'cursor' */
  225.         if(++TheCol>=NCOLS)
  226.           {/* next line */
  227.            TheCol = 0;
  228.            LeftCol = 0;
  229.            if(++TheRow>=NROWS) DoTheScroll();
  230.           }
  231.         else RightCol++;
  232.         break;
  233.      } /* end-switch */
  234.    } /* end-for */
  235.  /* compute invalid rectangle */
  236.  if((TopRow!=TheRow)||(LeftCol!=TheCol)||(RightCol!=TheCol))
  237.    {rect.left = CharWidth * LeftCol;
  238.     rect.top  = CharHeight * TopRow;
  239.     rect.right  = CharWidth * (RightCol+1);
  240.     rect.bottom = CharHeight * (TheRow+1);
  241.     InvalidateRect(hMainWnd,&rect,TRUE);
  242.    }
  243. }  /* end WriteTheString */
  244.  
  245. void DisplayLine(LPSTR Ptr)
  246. {WriteTheString(Ptr,lstrlen(Ptr));
  247.  WriteTheString(EndOfLine,1);
  248. } /* end DisplayLine */
  249.  
  250. void DisplayString(LPSTR Ptr)
  251. {WriteTheString(Ptr,lstrlen(Ptr));
  252. }
  253.  
  254. void DisplayChar(char Chr)
  255. {WriteTheString(&Chr,1);
  256. }  
  257.